<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Plugin API (GNU Compiler Collection (GCC) Internals)</title>

<meta name="description" content="Plugin API (GNU Compiler Collection (GCC) Internals)">
<meta name="keywords" content="Plugin API (GNU Compiler Collection (GCC) Internals)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Option-Index.html" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Plugins.html" rel="up" title="Plugins">
<link href="Plugins-pass.html" rel="next" title="Plugins pass">
<link href="Plugins-loading.html" rel="prev" title="Plugins loading">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>


</head>

<body lang="en">
<div class="section-level-extent" id="Plugin-API">
<div class="nav-panel">
<p>
Next: <a href="Plugins-pass.html" accesskey="n" rel="next">Interacting with the pass manager</a>, Previous: <a href="Plugins-loading.html" accesskey="p" rel="prev">Loading Plugins</a>, Up: <a href="Plugins.html" accesskey="u" rel="up">Plugins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Plugin-API-1"><span>24.2 Plugin API<a class="copiable-link" href="#Plugin-API-1"> &para;</a></span></h3>

<p>Plugins are activated by the compiler at specific events as defined in
<samp class="file">gcc-plugin.h</samp>.  For each event of interest, the plugin should
call <code class="code">register_callback</code> specifying the name of the event and
address of the callback function that will handle that event.
</p>
<p>The header <samp class="file">gcc-plugin.h</samp> must be the first gcc header to be included.
</p>
<ul class="mini-toc">
<li><a href="#Plugin-license-check" accesskey="1">Plugin license check</a></li>
<li><a href="#Plugin-initialization" accesskey="2">Plugin initialization</a></li>
<li><a href="#Plugin-callbacks" accesskey="3">Plugin callbacks</a></li>
</ul>
<div class="subsection-level-extent" id="Plugin-license-check">
<h4 class="subsection"><span>24.2.1 Plugin license check<a class="copiable-link" href="#Plugin-license-check"> &para;</a></span></h4>

<p>Every plugin should define the global symbol <code class="code">plugin_is_GPL_compatible</code>
to assert that it has been licensed under a GPL-compatible license.
If this symbol does not exist, the compiler will emit a fatal error
and exit with the error message:
</p>
<div class="example smallexample">
<pre class="example-preformatted">fatal error: plugin <var class="var">name</var> is not licensed under a GPL-compatible license
<var class="var">name</var>: undefined symbol: plugin_is_GPL_compatible
compilation terminated
</pre></div>

<p>The declared type of the symbol should be int, to match a forward declaration
in <samp class="file">gcc-plugin.h</samp> that suppresses C++ mangling.  It does not need to be in
any allocated section, though.  The compiler merely asserts that
the symbol exists in the global scope.  Something like this is enough:
</p>
<div class="example smallexample">
<pre class="example-preformatted">int plugin_is_GPL_compatible;
</pre></div>

</div>
<div class="subsection-level-extent" id="Plugin-initialization">
<h4 class="subsection"><span>24.2.2 Plugin initialization<a class="copiable-link" href="#Plugin-initialization"> &para;</a></span></h4>

<p>Every plugin should export a function called <code class="code">plugin_init</code> that
is called right after the plugin is loaded. This function is
responsible for registering all the callbacks required by the plugin
and do any other required initialization.
</p>
<p>This function is called from <code class="code">compile_file</code> right before invoking
the parser.  The arguments to <code class="code">plugin_init</code> are:
</p>
<ul class="itemize mark-bullet">
<li><code class="code">plugin_info</code>: Plugin invocation information.
</li><li><code class="code">version</code>: GCC version.
</li></ul>

<p>The <code class="code">plugin_info</code> struct is defined as follows:
</p>
<div class="example smallexample">
<pre class="example-preformatted">struct plugin_name_args
{
  char *base_name;              /* Short name of the plugin
                                   (filename without .so suffix). */
  const char *full_name;        /* Path to the plugin as specified with
                                   -fplugin=. */
  int argc;                     /* Number of arguments specified with
                                   -fplugin-arg-.... */
  struct plugin_argument *argv; /* Array of ARGC key-value pairs. */
  const char *version;          /* Version string provided by plugin. */
  const char *help;             /* Help string provided by plugin. */
}
</pre></div>

<p>If initialization fails, <code class="code">plugin_init</code> must return a non-zero
value.  Otherwise, it should return 0.
</p>
<p>The version of the GCC compiler loading the plugin is described by the
following structure:
</p>
<div class="example smallexample">
<pre class="example-preformatted">struct plugin_gcc_version
{
  const char *basever;
  const char *datestamp;
  const char *devphase;
  const char *revision;
  const char *configuration_arguments;
};
</pre></div>

<p>The function <code class="code">plugin_default_version_check</code> takes two pointers to
such structure and compare them field by field. It can be used by the
plugin&rsquo;s <code class="code">plugin_init</code> function.
</p>
<p>The version of GCC used to compile the plugin can be found in the symbol
<code class="code">gcc_version</code> defined in the header <samp class="file">plugin-version.h</samp>. The
recommended version check to perform looks like
</p>
<div class="example smallexample">
<pre class="example-preformatted">#include &quot;plugin-version.h&quot;
...

int
plugin_init (struct plugin_name_args *plugin_info,
             struct plugin_gcc_version *version)
{
  if (!plugin_default_version_check (version, &amp;gcc_version))
    return 1;

}
</pre></div>

<p>but you can also check the individual fields if you want a less strict check.
</p>
</div>
<div class="subsection-level-extent" id="Plugin-callbacks">
<h4 class="subsection"><span>24.2.3 Plugin callbacks<a class="copiable-link" href="#Plugin-callbacks"> &para;</a></span></h4>

<p>Callback functions have the following prototype:
</p>
<div class="example smallexample">
<pre class="example-preformatted">/* The prototype for a plugin callback function.
     gcc_data  - event-specific data provided by GCC
     user_data - plugin-specific data provided by the plug-in.  */
typedef void (*plugin_callback_func)(void *gcc_data, void *user_data);
</pre></div>

<p>Callbacks can be invoked at the following pre-determined events:
</p>

<div class="example smallexample">
<pre class="example-preformatted">enum plugin_event
{
  PLUGIN_START_PARSE_FUNCTION,  /* Called before parsing the body of a function. */
  PLUGIN_FINISH_PARSE_FUNCTION, /* After finishing parsing a function. */
  PLUGIN_PASS_MANAGER_SETUP,    /* To hook into pass manager.  */
  PLUGIN_FINISH_TYPE,           /* After finishing parsing a type.  */
  PLUGIN_FINISH_DECL,           /* After finishing parsing a declaration. */
  PLUGIN_FINISH_UNIT,           /* Useful for summary processing.  */
  PLUGIN_PRE_GENERICIZE,        /* Allows to see low level AST in C and C++ frontends.  */
  PLUGIN_FINISH,                /* Called before GCC exits.  */
  PLUGIN_INFO,                  /* Information about the plugin. */
  PLUGIN_GGC_START,             /* Called at start of GCC Garbage Collection. */
  PLUGIN_GGC_MARKING,           /* Extend the GGC marking. */
  PLUGIN_GGC_END,               /* Called at end of GGC. */
  PLUGIN_REGISTER_GGC_ROOTS,    /* Register an extra GGC root table. */
  PLUGIN_ATTRIBUTES,            /* Called during attribute registration */
  PLUGIN_START_UNIT,            /* Called before processing a translation unit.  */
  PLUGIN_PRAGMAS,               /* Called during pragma registration. */
  /* Called before first pass from all_passes.  */
  PLUGIN_ALL_PASSES_START,
  /* Called after last pass from all_passes.  */
  PLUGIN_ALL_PASSES_END,
  /* Called before first ipa pass.  */
  PLUGIN_ALL_IPA_PASSES_START,
  /* Called after last ipa pass.  */
  PLUGIN_ALL_IPA_PASSES_END,
  /* Allows to override pass gate decision for current_pass.  */
  PLUGIN_OVERRIDE_GATE,
  /* Called before executing a pass.  */
  PLUGIN_PASS_EXECUTION,
  /* Called before executing subpasses of a GIMPLE_PASS in
     execute_ipa_pass_list.  */
  PLUGIN_EARLY_GIMPLE_PASSES_START,
  /* Called after executing subpasses of a GIMPLE_PASS in
     execute_ipa_pass_list.  */
  PLUGIN_EARLY_GIMPLE_PASSES_END,
  /* Called when a pass is first instantiated.  */
  PLUGIN_NEW_PASS,
/* Called when a file is #include-d or given via the #line directive.
   This could happen many times.  The event data is the included file path,
   as a const char* pointer.  */
  PLUGIN_INCLUDE_FILE,

  /* Called when -fanalyzer starts. The event data is an
     ana::plugin_analyzer_init_iface *.  */
  PLUGIN_ANALYZER_INIT,

  PLUGIN_EVENT_FIRST_DYNAMIC    /* Dummy event used for indexing callback
                                   array.  */
};
</pre></div>

<p>In addition, plugins can also look up the enumerator of a named event,
and / or generate new events dynamically, by calling the function
<code class="code">get_named_event_id</code>.
</p>
<p>To register a callback, the plugin calls <code class="code">register_callback</code> with
the arguments:
</p>
<ul class="itemize mark-bullet">
<li><code class="code">char *name</code>: Plugin name.
</li><li><code class="code">int event</code>: The event code.
</li><li><code class="code">plugin_callback_func callback</code>: The function that handles <code class="code">event</code>.
</li><li><code class="code">void *user_data</code>: Pointer to plugin-specific data.
</li></ul>

<p>For the <i class="i">PLUGIN_PASS_MANAGER_SETUP</i>, <i class="i">PLUGIN_INFO</i>, and
<i class="i">PLUGIN_REGISTER_GGC_ROOTS</i> pseudo-events the <code class="code">callback</code> should be null,
and the <code class="code">user_data</code> is specific.
</p>
<p>When the <i class="i">PLUGIN_PRAGMAS</i> event is triggered (with a null pointer as
data from GCC), plugins may register their own pragmas.  Notice that
pragmas are not available from <samp class="file">lto1</samp>, so plugins used with
<code class="code">-flto</code> option to GCC during link-time optimization cannot use
pragmas and do not even see functions like <code class="code">c_register_pragma</code> or
<code class="code">pragma_lex</code>.
</p>
<p>The <i class="i">PLUGIN_INCLUDE_FILE</i> event, with a <code class="code">const char*</code> file path as
GCC data, is triggered for processing of <code class="code">#include</code> or
<code class="code">#line</code> directives.
</p>
<p>The <i class="i">PLUGIN_FINISH</i> event is the last time that plugins can call GCC
functions, notably emit diagnostics with <code class="code">warning</code>, <code class="code">error</code>
etc.
</p>

</div>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Plugins-pass.html">Interacting with the pass manager</a>, Previous: <a href="Plugins-loading.html">Loading Plugins</a>, Up: <a href="Plugins.html">Plugins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
